<?php
require_once(APPPATH."models/base.php");

class usergroup_model extends base_model 
{
	private function get_children_by_id($id)
	{
		$usergroups = array();
		$this->db->order_by('order asc,id asc')  ; 
		$db_usergroups = 
			$this->db->get_where('usergroup',array('parent_id'=>$id))->result(); 
		// print_r($db_usergroups);exit; 
		foreach($db_usergroups as $db_usergroup)
		{
			$usergroups[] = $this->convert($db_usergroup);
		}
		return $usergroups;
	}
	
	public function get_acls()
	{
		return $this->db->get_where("acl",array("usergroup_id"=>$this->id))->result();
	}
	
	public function permission($resource,$action)
	{
		if($this->type=="supermanager") {
			return true;
		}
		$acls = $this->acls;
		foreach($acls as $acl)
		{
			if($acl->resource == $resource && $acl->action == $action)
			{
				return true ; 
			}
		}
		return false;
	}
	
	public function get_parent()
	{
		return $this->usergroup_manager->get($this->parent_id);
	}
	
	public function get_children()
	{
		return $this->get_children_by_id($this->id);
	}
	
	public function get_brothers()
	{
		// echo $this->parent_id;exit;
		return $this->get_children_by_id($this->parent_id);
	}
	
	private $descendants = array();
	private function bl_descendant($class)
	{
		$children = $class->children;
		//p_r($children);exit;
		
		foreach($children as $child)
		{
			$this->descendants[] = $child;
			//p_r($ds);exit;
			if($child->children != null)
			{
				$this->bl_descendant($child);
			}
		}
	}
	public function get_descendant()
	{
		$this->descendants = array();
		//echo "ddd";  
		$this->bl_descendant($this);
		// p_r($this->descendants);exit;
		return $this->descendants;
	}
	
	public function get_ancestors()
	{
		$class = $this;
		$parent = $class->parent;
		$as = array();
		while ($parent->id!=0)
		{
			$as[] = $parent;
			$parent = $parent->parent;
		}
		return array_reverse($as);  
	}
}

class usergroup_manager extends base_manager
{ 
	function insert($obj)
	{
		$acls = array_remove($obj, "acls"); 
		$this->db->trans_begin(); 
		$this->db->insert($this->table_name,$obj); 
		$id =  $this->db->insert_id();
		$this->db->delete("acl",array("usergroup_id"=>$id));
		foreach($acls as $acl)
		{
			$acl->usergroup_id = $id;
			$this->db->insert("acl",$acl) ;
		}
		 
		if ($this->db->trans_status() === FALSE)
		{
			$this->db->trans_rollback();
			return null;
		}
		else
		{
			$this->db->trans_commit();	
			return $id ;   
		}
	}
	
	function update($obj,$params)
	{
		$acls = array_remove($obj, "acls"); 
		// print_r($acls);exit ; 
		$this->db->trans_begin();
		if(is_array($params))
		{
			$this->db->update($this->table_name,$obj,$params);
			if(isset($params["id"]))
			{
				$this->db->delete("acl",array("usergroup_id"=>$params["id"]));
				foreach($acls as $acl)
				{
					$acl->usergroup_id = $params["id"]; 
					$this->db->insert("acl",$acl) ; 
				}
			}
		}
		else
		{
			$this->db->update($this->table_name,$obj,array('id'=>$params));  
			$this->db->delete("acl",array("usergroup_id"=>$params));
			foreach($acls as $acl)
			{
				$acl->usergroup_id = $params ; 
				$this->db->insert("acl",$acl) ; 
			}
		}
		  
		
		 
		if ($this->db->trans_status() === FALSE)
		{
			$this->db->trans_rollback();
		}
		else
		{
			$this->db->trans_commit(); 
		}
	}
	
	public function get($params ,$load_type = Load_Type::Lazy)
	{
		$usergroup = parent::get($params);
		
		if($load_type == Load_Type::Normal)
		{
			$usergroup->parent = $usergroup->parent;
			$usergroup->children = $usergroup->children;
			$usergroup->brothers = $usergroup->brothers;
			$usergroup->descendant = $usergroup->descendant;  
			$usergroup->ancestors = $usergroup->ancestors;
		}
		
		return $usergroup;
	}
	
	public function get_root_list()
	{
		// echo $sql;exit;
		$this->db->order_by('order asc,id desc') ;     
		$db_usergroupes = 
			$this->db->get_where('usergroup',array('parent_id'=>0))->result();
		// print_r($db_articles);exit;
		$usergroupes = array();
		foreach($db_usergroupes as $db_usergroup)
		{
			$usergroupes[] = $this->convert($db_usergroup);
		}
		return $usergroupes ; 
	}
	
	private function bl_delete($id)
	{
		$usergroup = $this->get($id);
		if(sizeof($usergroup->children)>0)
		{
			foreach($usergroup->children as $class)
			{
				$this->bl_delete($class->id);
			}
		} 
		$this->db->delete('user',array('usergroup_id'=>$id));  
		$this->db->delete('usergroup',array('id'=>$id));
	}
	
	public function delete($id)  
	{
		$this->db->trans_begin();
		$this->bl_delete($id);
		if ($this->db->trans_status() === FALSE)
		{
			$this->db->trans_rollback();
		}
		else
		{
			$this->db->trans_commit();
		}
	}
}

?>